/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.integraldetail.service;

import java.util.List;

import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.member.entity.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.integraldetail.entity.IntegralDetail;
import com.jeeplus.modules.integraldetail.mapper.IntegralDetailMapper;

/**
 * 积分明细Service
 * @author w
 * @version 2021-04-06
 */
@Service
@Transactional(readOnly = true)
public class IntegralDetailService extends CrudService<IntegralDetailMapper, IntegralDetail> {
	@Autowired
	private AccountService accountService;

	public IntegralDetail get(String id) {
		return super.get(id);
	}
	
	public List<IntegralDetail> findList(IntegralDetail integralDetail) {
		return super.findList(integralDetail);
	}
	
	public Page<IntegralDetail> findPage(Page<IntegralDetail> page, IntegralDetail integralDetail) {
		return super.findPage(page, integralDetail);
	}
	
	@Transactional(readOnly = false)
	public void save(IntegralDetail integralDetail) {
		super.save(integralDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(IntegralDetail integralDetail) {
		super.delete(integralDetail);
	}

	public List<IntegralDetail> findListByDays(IntegralDetail integralDetail) {
		return mapper.findListByDays(integralDetail);
	}

	public int findVideoByDay(IntegralDetail integralDetail) {
		return mapper.findVideoByDay(integralDetail);
	}

	@Transactional(readOnly = false)
	public void saveByVideo(Member member) {
		Account account = accountService.get(member.getAccount().getId());
		if (account != null){
			account.setIntegral(account.getIntegral() + 2);
			accountService.save(account);
			//记录
			SnowFlake snowFlake = new SnowFlake(2,3);
			Long ordersNo = snowFlake.nextId();
			IntegralDetail integralDetail = new IntegralDetail();
			integralDetail.setMember(member);
			integralDetail.setIntegralType("4");
			integralDetail.setIntegral(2.0);
			integralDetail.setOrderNo(ordersNo.toString());
			save(integralDetail);
		}else {
			throw new RuntimeException("账户信息有误");
		}
	}

	public List<IntegralDetail> findListLimit(IntegralDetail integralDetail) {
		return mapper.findListLimit(integralDetail);
	}
}